package org.quickserver.net.GB268753QN.controlSzgw;

import java.text.SimpleDateFormat;
import java.util.*;

import org.quickserver.net.model.DevType;
import org.quickserver.net.model.GB268753QNSZGW;
import org.quickserver.net.model.GBServerDataAll;
import org.quickserver.net.model.GBServerDataSZGW;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.util.data.dataProcessing;
import org.quickserver.util.state.QNA2B2State;

public class QNA2B2 {
	
	public QNA2B2 () {}
	
	public static List<GB268753QNSZGW> QNA2B2OBJ(ClientHandler handler, String strs[],String ydz)
    {
    	// 获取设备码
    	String uniCode=dataProcessing.hexTo10StrAll(ydz);
    	GBServerDataSZGW ec = (GBServerDataSZGW)handler.getClientData();
        ec.setUniqueCode(uniCode);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取应用数据单元条数
        int total = dataProcessing.hexStringToInt10(strs[1]);
        List<GB268753QNSZGW> Glist = new ArrayList<GB268753QNSZGW>();
        
        for (int i = 0; i < total; i++) {
        	// 根据每条长度
        	int fors = i * 46;
        	// 触发时间
        	String stateTime[] = new String[6];
            stateTime[0] = strs[42 + fors];
            stateTime[1] = strs[43 + fors];
            stateTime[2] = strs[44 + fors];
            stateTime[3] = strs[45 + fors];
            stateTime[4] = strs[46 + fors];
            stateTime[5] = strs[47 + fors];
            // 系统类型标志
            String sysType[] = new String[1];
            sysType[0] = strs[2 + fors];
            // 系统地址
            String sysAddress[] = new String[1];
            sysAddress[0] = strs[3 + fors];
            // 部件类型
            String componentType[] = new String[1];
            componentType[0] = strs[4 + fors];
            // 部件地址
            String componentAddress[] = new String[1];
            componentAddress[0] = strs[8 + fors] + strs[7 + fors] + strs[6 + fors] + strs[5 + fors];
            // 部件状态
            String componentState[] = new String[2];
            componentState[0] =strs[9 + fors];
            componentState[1]=strs[10 + fors];

            Map<String, String> map =getStatic(componentState);

            // 部件说明
            int length = strs.length;
            String componentExplain[] = new String[1];
            for(int k = 0; k < length; k++) {
            	if (k > 10 + fors && k < 42 + fors) {
            		if (componentExplain[0] == null) {
    					componentExplain[0] = strs[i];
    				}
    				componentExplain[0] = componentExplain[0] + strs[i];
    			}
            }
         // 封装
            GB268753QNSZGW SZGW = new GB268753QNSZGW();
            SZGW.setId(ec.getUniqueCode());
            SZGW.setUniqueCode(ec.getUniqueCode());
            SZGW.setUploadTime(sdf.format(new Date()));
            SZGW.setStateTime(dataProcessing.strsToTime(stateTime));
            SZGW.setSysType(dataProcessing.hexStringToInt10(sysType[0]) + "");
            SZGW.setSysAddress(dataProcessing.hexStringToInt10(sysAddress[0]) + "");
            SZGW.setComponentType(dataProcessing.hexStringToInt10(componentType[0]) + "");
            SZGW.setComponentAddress(dataProcessing.strTo10StrAll(componentAddress[0]));
            SZGW.setComponentExplain(dataProcessing.strTo10StrAll(componentExplain[0]));
            SZGW.setComponentState(dataProcessing.strTo10StrAll(componentState[0]));
            SZGW.setComponentStateMap(map);
            Glist.add(SZGW);
		}
        System.out.println("kgl============"+Glist.toString()+"===========");
        Object obj = GBServerDataAll.devMap.get(uniCode);
        if(obj != null)
            ec.setDevType(obj.toString());
        GBServerDataAll.handlerMap.put(uniCode, handler);
        return Glist;
    }

         public  static Map getStatic(String[] strs){
             Map<String, String> map = new HashMap();
             int[] d = dataProcessing.henTo2(strs[0]);
             int[] g = dataProcessing.henTo2(strs[1]);
             int strLen1 = d.length;
             int strLen2 = g.length;
             d = Arrays.copyOf(d, strLen1 + strLen2);
             System.arraycopy(g, 0, d, strLen1, strLen2);
             for (int i = 0; i < d.length; ++i) {
                 switch (i) {
                     case 0:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.ys.getCode(), DevType.ys.getName());
                                 continue;
                         }
                     case 1:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.fk.getCode(), DevType.fk.getName());
                                 continue;
                         }
                     case 2:
                         switch (d[i]) {
                             case 0:
                                 map.put(DevType.tz.getCode(), DevType.tz.getName());
                                 continue;
                             case 1:
                                 map.put(DevType.qd.getCode(), DevType.qd.getName());
                             default:
                                 continue;
                         }
                     case 3:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.jg.getCode(), DevType.jg.getName());
                                 continue;
                         }
                     case 4:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.pb.getCode(), DevType.pb.getName());
                                 continue;
                         }
                     case 5:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.gz.getCode(), DevType.gz.getName());
                                 continue;
                         }
                     case 6:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.hj.getCode(), DevType.hj.getName());
                                 continue;
                         }
                     case 7:
                         switch (d[i]) {
                             case 0:
                                 map.put(DevType.zcyx.getCode(), DevType.zcyx.getName());
                                 continue;
                             case 1:
                                 map.put(DevType.csyx.getCode(), DevType.csyx.getName());
                             default:
                                 continue;
                         }
                     case 8:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 map.put(DevType.gzhf.getCode(), DevType.gzhf.getName());
                                 continue;
                         }
                     case 9:
                         switch (d[i]) {
                             case 0:
                             case 1:
                             default:
                                 continue;
                         }
                     case 10:
                         switch (d[i]) {
                             case 0:
                             case 1:
                             default:
                                 continue;
                         }
                     case 11:
                         switch (d[i]) {
                             case 0:
                             case 1:
                             default:
                                 continue;
                         }
                     case 12:
                         switch (d[i]) {
                             case 0:
                             default:
                                 continue;
                             case 1:
                                 continue;
                         }
                     case 13:
                         switch (d[i]) {
                             case 0:
                             case 1:
                             default:
                                 continue;
                         }
                     case 14:
                         switch (d[i]) {
                             case 0:
                             case 1:
                             default:
                                 continue;
                         }
                     case 15:
                         switch (d[i]) {
                             case 0:
                                 map.put(DevType.dyzc.getCode(), DevType.dyzc.getName());
                                 break;
                             case 1:
                                 map.put(DevType.dygz.getCode(), DevType.dygz.getName());
                         }
                 }
             }
             if (null != map.get(DevType.pb.getCode()) && null != map.get(DevType.gzhf.getCode())) {
                 map.remove(DevType.gzhf.getCode());
                 map.put(DevType.pbjc.getCode(), DevType.pbjc.getName());
             }
             if (null != map.get(DevType.qd.getCode()) && null != map.get(DevType.gzhf.getCode())) {
                 map.remove(DevType.gzhf.getCode());
                 map.put(DevType.tz.getCode(), DevType.tz.getName());
             }

             return map;
         }
}
